$(function() {
	//判断退换货缓存是否存在
	var obj = JSON.parse(localStorage.getItem('goods_info'));
	console.log(obj);
	var shuxing = obj.shuxingzhi;
	var img = obj.img;
	var title = obj.title;
	var num1 = obj.num;
	var price = obj.price;
	var branchid = obj.branchid;
	var orderid = obj.orderid;
	var point = obj.point;
	showData(shuxing,img,title);
	console.log(price,num1);
	//将缓存的数据展示到页面上
	function showData(shuxingzhi, img, title) {
		var show = $(".show");
		var goods_detail = $('<div class="goods"><img src= ' + img + ' /></div><div class="detail_info"><p>' + title + '</p><p>' + shuxingzhi + '</p></div>');
		show.append(goods_detail);
	}
	//将缓存的价钱显示到页面
	$(".red_num").children("span").text(price * num1);
	$(".most").text(price);
	
	console.log(point);
	if(point !=""){
		point = point - 0;
		$("#red_point").css("display","block");
		$("#red_point").children("span").text(point * num1);
		
	}else{
		$("#red_point").css("display","none");
	}
	
	//点击加数量增加
	num1 = num1 - 0;
	var num = $(".num").text() - 0;
	$(".add").click(function() {
		if(num == num1) {
			pop("超过买的总数，不能添加");
			return false;
		}
		num++;
		$(".num").text(num);
		$(".red_num").children("span").text(price * num);
		//判断集分是否存在，点击计算集分的变化
		if(point != ""){
			point = point - 0;
			$("#red_point").css("display","block");
			$("#red_point").children("span").text(point * num);
		}else{
			$("#red_point").css("display","none");
		}

	});
	//点击数量减少
	$(".reduce").click(function() {
		num--;
		if(num < 1) {
			num = 1;
		}
		$(".num").text(num);
		$(".red_num").children("span").text(price * num);
		//判断集分是否存在，点击计算集分的变化
		if(point != ""){
			point = point - 0;
			$("#red_point").css("display","block");
			$("#red_point").children("span").text(point * num);
		}else{
			$("#red_point").css("display","none");
		}
		
		
	});

	//存储元素的内容
	var reason_content;

	$(".refund_content .reason .refund_reason p:first-child span:last-child").click(function() {
		$(".reason_refund").css("display", "block");
		$(".refund_info").hide();
		$(".upload_credentials").hide();
		$(".refund_footer").hide();
		$("#mengban").css("display", "block");
	});

	//当点击单选时调用这个方法
	$("input:radio").click(function() {
		radio();
	});
	//点击单选框，改变label的样式
	function radio() {
		var radioArr = $("input:radio");
		for(var i = 0; i < radioArr.length; i++) {
			if(radioArr[i].checked) {
				radioArr[i].nextSibling.setAttribute("class", "change");
			} else {
				radioArr[i].nextSibling.setAttribute("class", "");
			}
		}
	}
	radio();

	//点击底部关闭，将选择的内容放在页面上
	$(".footer").click(function() {
		reason_content = $("input[type = radio]:checked").siblings().parent().siblings().text();
		$(".refund_content .reason .refund_reason p:last-child").text(reason_content);
		$(".reason_refund").css("display", "none");
		$(".refund_info").show();
		$(".upload_credentials").show();
		$(".refund_footer").show();
		$("#mengban").hide();
	});

	var fileElem = $("#fileElem"); //上传按钮
	var fileList = $("#fileList"); //盛放容器
	var ids = []; //存放图片的id

	//图片选择事件
	fileElem.change(function() {
		var files = this.files;
		console.log(files);
		var sibling = $(".img-list").children().length;
		if(sibling + files.length >= 7) { //     限制图片上传张数   不能超过6张
			pop("图片不能超过六张");
			fileElem.prop("disabled", true);
			$(".tuku").hide();
		} else { //上传图片
			postImgData(files);
		}
	});

	// 上传图片时提交表单
	function postImgData(files) {
		var formData = new FormData($("#uploadForm")[0]);
		console.log(formData);
		$.ajax({
			url: "http://a.91jfk.com/Product/Ordershop/ImgUpload",
			type: 'post',
			cache: false,
			data: formData,
			processData: false,
			contentType: false,
			success: function(xhr) {
				console.log(xhr);
				var data = JSON.parse(xhr);
				if(data.code == 1) {
					var imgs = data.message;
					console.log(imgs);
					$.each(imgs, function(i) {
						var div = $("<div class='img fl'></div>");
						var span = $("<span class='del'>—</span>");
						var input = $("<input type='hidden' value=" + imgs[i].id + ">");
						var img = $('<img src= "' +imgs[i].src + '">');
						div.append(span).append(input).append(img);
						fileList.append(div);
						ids.push(imgs[i].id);
						//删除图片
						span.bind("click", function() {
							var that = $(this);
							var delId = $(this).next().val();
							$.ajax({
								url: "http://a.91jfk.com/Product/Ordershop/delpic",
								type: "post",
								data: {
									id: delId
								},
								datatype: 'json',
								success: function(xhr) {
									var data = JSON.parse(xhr);
									if(data.code == 1) {
										that.parent().remove();
										ids.slice(1, 1);
									}
								}
							})
						})
					})
				} else {
					pop(data.message);
				}
			},
			error: function(xhr) {
				//var data=JSON.parse(xhr);
				var data = xhr;
				pop(data.message);
			}
		})

	}
	//提交之后进行判断
	$(".refund_footer").click(function(e) {
			//阻止冒泡
			e.preventDefault();
			var storage = window.localStorage;
			storage.setItem("bid",branchid);
			storage.setItem("oid",orderid);
			var id = ids.join(",");
			console.log(id);
			console.log(reason_content);
			console.log(num);
			//补充说明
			var refund_descript = $(".refund_info ul li:last-child input").val();
			console.log(refund_descript);
			if(reason_content == ""){
				pop("退款原因不能为空！");
			}else{
				$.ajax({
					url: 'http://a.91jfk.com/Product/Ordershop/ApplyForReturn',
					type: 'post',
					data: {
						branchid: branchid,
						orderid: orderid,
						reason:reason_content,
						pic_id:id,
						num:num,
						refund_descript:refund_descript
					},
					dataType: 'json',
					success: function(xhr){
						console.log(xhr);
					if(xhr.code == "1") {
						pop("提交成功！");
						setTimeout(function() {
							window.location.href = "pending.html";
						}, 500);
						
					} else {
						pop(xhr.message);
						pop(xhr.message);
					}
				},
				error: function(res) {
					pop(res);
					
				}
			});
			}
			

	});

//提示弹框
function pop(a) {
	$(".errAlert").text(a).addClass("AlertShow");
	setTimeout(function() {
		$(".errAlert").removeClass("AlertShow");
	}, 1000);
}

});